#encoding:utf8
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.http import HttpResponse
from book.models import *
from settings import STATIC_ROOT
import datetime
from django.views.decorators.http import require_POST, require_GET

import sys
import getopt
import jieba
import jieba.posseg as pseg
from collections import defaultdict

ENCODING = 'utf-8'
POSITIVE = '1'
NEGATIVE = '0'
PRIVATIVE = 'ne'
_TOPIC = 't'
ZHENGTIGANJUE = u'整体感觉'
BUCUO = u'不错'
YIBAN = u'一般'
ALLCLASSID = '-1'

jieba.load_userdict('../data/fenci.txt')

def load_model(topic_file, negative_file, positive_file, privative_file, tagfile):
    #         0       1     2      3  4
    # model = classid:topic:words:'p':sentiments

    classid_index, topic_index, words_index, sentiment_class_index, sentiment_index = 0, 1, 2, 3, 4
    topic_dict = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(set)))) 
    for line in open(topic_file).readlines():
        l = line.strip('\n').decode(ENCODING).split(',')
        topic_dict[l[classid_index]][l[topic_index]][l[words_index]][l[sentiment_class_index]].add(l[sentiment_index].strip())
    # each line a word
    sentiments_dict = defaultdict(set)
    for line in open(positive_file).readlines():
        sentiments_dict[POSITIVE].add(line.strip().decode(ENCODING))
    for line in open(negative_file).readlines():
        sentiments_dict[NEGATIVE].add(line.strip().decode(ENCODING))
    for line in open(privative_file).readlines():
        sentiments_dict[PRIVATIVE].add(line.strip().decode(ENCODING))                  

    # classid : topic : sentiment_class : sentiment_name
    sentiment_class_index, sentiment_name_index = 3, 2
    tags_dict = defaultdict(lambda: defaultdict(lambda: defaultdict()))
    for line in open(tagfile).readlines():
        l = line.decode(ENCODING).split('\t')
        tags_dict[l[classid_index]][l[topic_index]][l[sentiment_class_index].strip()] = l[sentiment_name_index].strip()
    tags_dict[ALLCLASSID][ZHENGTIGANJUE][POSITIVE] = BUCUO
    tags_dict[ALLCLASSID][ZHENGTIGANJUE][NEGATIVE] = YIBAN 
    return topic_dict, sentiments_dict, tags_dict 



def classify(arguments, sep='\t', classid_index=1, comment_index=2, test=False):
    topic_dict, sentiments_dict, tags_dict = load_model('../data/attribute.csv','../data/negative_words','../data/positive_words','../data/privative_words','../data/tags.txt')

    total,reco,right = 0,0,0
    result = []
    for line in arguments:
        l = line.decode(ENCODING).split(sep) 
        classid = l[classid_index]
        # tags = topic:sentiment:sentiment_words
        words = [ w.lower() for w in jieba.cut(l[comment_index].strip()) if not w.isdigit() ]
        #topics = [ (_TOPIC, topic, words.index(w)) for topic in topic_dict[classid] for w in words if w in topic_dict[classid][topic] ]
        topics = []
        temp_dict = {} 
        for topic in topic_dict[classid]:
            for w in words:
                if w in topic_dict[classid][topic]:
                    #print w,topic
                    topics.append((_TOPIC, topic, words.index(w)))
                    if w not in temp_dict:
                        temp_dict[topic] = topic_dict[classid][topic][w]
        tag_list = judge_sentiment(words, temp_dict, classid, sentiments_dict, topics, tags_dict) 

        total += 1
        if test:
            flag = True
            for tag in tag_list:
                if l[0] == tag:
                    right += 1
                    flag = False
            if flag:
                for tag in tag_list:
                    #print 'error' + tag.encode(ENCODING) + '\t' + line
                    pass

        if len(tag_list) > 0:
            reco += 1
            result.append( '\t'.join(tag_list).encode(ENCODING) + '\t' + line)
            pass
        else:
            pass
            #print line
        
    if test:
        print str(total)+'/'+str(reco)+'/'+str(right)
        print reco/float(total),right/float(reco)

    return result

def judge_sentiment(words, topic_dict, classid, sentiments_dict, topics, tags_dict):
    result = [] 
    pr, po, ne = [], [], []
    for w in words:
        f = True
        for key,topic,index in topics:
            if w in topic_dict[topic][POSITIVE]:
                po.append((POSITIVE, w, words.index(w)))
                f = False
            elif w in topic_dict[topic][NEGATIVE]:
                ne.append((NEGATIVE, w, words.index(w)))
                f = False
        if w in sentiments_dict[PRIVATIVE]: 
            pr.append((PRIVATIVE, w, words.index(w)))
        elif w in sentiments_dict[POSITIVE] and f:
            po.append((POSITIVE, w, words.index(w)))
        elif w in sentiments_dict[NEGATIVE] and f:
            ne.append((NEGATIVE, w, words.index(w)))
                
    sentence = pr + po + ne + topics
    sentence.sort(key = lambda tup:tup[2])
    tag_l,pri,nei = [],[],[]
    senti = (POSITIVE, NEGATIVE)
    if len(topics) == 0:
        if len(pr)%2 != 0:
            senti = (NEGATIVE, POSITIVE)
        if len(po) > 0:
            result.append(ZHENGTIGANJUE + tags_dict[ALLCLASSID][ZHENGTIGANJUE][senti[0]])
        elif len(nei) > 0:
            result.append(ZHENGTIGANJUE + tags_dict[ALLCLASSID][ZHENGTIGANJUE][senti[1]]) 
    else:
        for x,y,z in sentence:
            if x == _TOPIC:
                tag_l.append(y)
            elif x == PRIVATIVE:
                nei.append(x)
            elif x == POSITIVE:
                if len(nei)%2 != 0:
                    senti = (NEGATIVE, POSITIVE)
                for t in tag_l:
                    result.append(t+tags_dict[classid][t][senti[0]])
                tag_l = []
                nei = []
                senti = (POSITIVE, NEGATIVE)
            elif x == NEGATIVE:
                if len(nei)%2 != 0:
                    senti = (NEGATIVE, POSITIVE)
                for t in tag_l:
                    result.append(t+tags_dict[classid][t][senti[1]])
                tag_l = []
                nei = []
                senti = (POSITIVE, NEGATIVE)

    return result
    # judge privative

def mains(argv):
        try:
            opts, args = getopt.getopt(argv, 'i:c:s:t')
        except getopt.GetoptError:
            sys.exit(2)

        classid_index = 1
        comment_index = 2
        sep = '\t'
        test = False
        for opt, arg in opts:
            if opt == '-i':
                classid_index = int(arg)
            elif opt == '-c':
                comment_index = int(arg)
            elif opt == '-s':
                sep = arg
                if sep == '\\t':
                    sep = '\t'
            elif opt == '-t':
                test = True

        result.append( classify(sep, classid_index, comment_index, test))

def deal_comments(topic):
        import re
        ENCODING = 'utf-8'
        result = []
    	
        '''替换中文,a-z,A-Z,0-9之外的所有字符'''
        regex = re.compile(ur"[^\u4e00-\u9fa5a-zA-Z0-9]")
        li =  regex.sub("\t", topic).strip().split('\t')
        for i in li:
            result.append("test"+"\t"+"655"+"\t"+i.encode(ENCODING))
        return result


def index(request, template):
    import commands
    import time
    errors = []
    if request.POST.has_key('review'):
        review = request.POST['review']
        if review == "":
            errors.append('评论不能为空噢！')
        else:
            group = str(time.time())
            ret = classify(deal_comments(review))
            for line in ret:
                    line = line.strip()
                    if line == "":
                        continue
                    line = line.split()
                    tag,topic = line[:-3], line[-1].decode('utf8') 
                    if topic in review:
                        Comment = review.replace(topic, "<span style='color:#FFFFFF;background-color:#009900;'>%s</span>" % topic)
                        for h in tag:
                            h = h.decode('utf8')
                            p1 = reviews(None, h, Comment,'0', group)
                            p1.save()
           
    if request.GET.has_key('check'):
        check = request.GET['check']
        Id = request.GET['id']
        modify = reviews.objects.get(id=Id)
        modify.flag=check
        modify.save()
    if 'group' in locals():
        reviewsInfo = reviews.objects.filter(group=group).values()
        if list(reviewsInfo) == []:
            errors.append('这个评论没有被覆盖，请换一个试试')
    else:
        reviewsInfo = reviews.objects.all().values()[::-1]
    wrong = len(reviews.objects.filter(flag='2').values())
    right = len(reviews.objects.filter(flag='1').values())
    unknown = len(reviews.objects.filter(flag='0').values())
    total = wrong+right+unknown
    return render_to_response(template, locals(), context_instance=RequestContext(request))



@require_GET
def tags(request, template):
    goodtags = set([i.tag for i in reviews.objects.iterator()])
    #badtags = set([i.tag for i in reviews.objects.iterator()])
    if 'tag' in request.GET:
        tag = request.GET['tag'].strip()
        reviewsInfo = reviews.objects.filter(tag=tag).values()[::-1]
    return render_to_response(template, locals())


